<template>
  <div>
    <el-dialog
      :title="isUpdate ? '编辑' : '新建'"
      v-model="props.visible"
      width="50%"
      @close="closeDialog()"
    >
      <el-form
        :model="form"
        label-position="right"
        label-width="100px"
        ref="formRef"
        @submit.prevent
        :rules="rules"
      >
        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="库名：" prop="dbName">
              <el-input
                placeholder="请输入库名"
                v-model="form.dbName"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="表名：" prop="tableName">
              <el-input
                placeholder="请输入表名"
                v-model="form.tableName"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="查询sql：" prop="tableName">
              <el-input
                placeholder="请输入sql"
                type="textarea"
                v-model="form.querySql"
                resize="none"
                :rows="6"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="删除主键：" prop="deleteKey">
              <el-input
                placeholder="请输入主键"
                v-model="form.deleteKey"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="18" :offset="1">
            <el-form-item label="归属模块：" prop="modelName">
              <el-radio-group v-model="form.modelName">
                <el-radio :value="1">新核心</el-radio>
                <el-radio :value="2">老核心</el-radio>
                <el-radio :value="3">emc会员</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="18" :offset="1">
            <el-form-item label="归属模块：" prop="modelName">
              <el-switch
                v-model="form.disable"
                inline-prompt
                active-text="启用"
                inactive-text="关闭"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="17" :offset="1">
            <el-form-item label="备注：" prop="notes">
              <el-input
                placeholder=""
                type="textarea"
                v-model="form.notes"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-row :gutter="20"
          ><el-col :span="17" :offset="1">
            <div
              class="dialog-footer"
              style="display: flex; justify-content: left; margin-bottom: 30px"
            >
              <el-button type="primary" @click="save()">确 定</el-button>
              <el-button @click="closeDialog()">取 消</el-button>
            </div>
          </el-col>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { kfApi } from "@/api/kernel/kf-config.js";

const { proxy } = getCurrentInstance();

const formRef = ref(null);
const props = defineProps({
  visible: Boolean,
  data: Object,
});
const emits = defineEmits(["update:visible", "done"]);

const form = ref({
  modelName: 1,
  disable: true,
});
const rules = ref({
  dbName: [{ required: true, message: "库名不能为空", trigger: "blur" }],
  tableName: [{ required: true, message: "表名不能为空", trigger: "blur" }],
  querySql: [{ required: true, message: "查询sql不能为空", trigger: "blur" }],
  deleteKey: [{ required: true, message: "查询sql不能为空", trigger: "blur" }],
});
onMounted(async () => {
  if (props.data) {
    console.log("修改===========");
    getInfoById(props.data);
    isUpdate.value = true;
  } else {
    console.log("添加=============");
    isUpdate.value = false;
  }
});
const getInfoById = (data) => {
  let { id } = data;
  kfApi.getInfoById(id).then((res) => {
    let { modelName } = res.data;
    let modelNameInt = parseInt(modelName);
    form.value = Object.assign({}, res.data);
    form.value.modelName = modelNameInt;
  });
};

// 是否是编辑状态
const isUpdate = ref(false);

const closeDialog = () => {
  formRef.value.resetFields();
  updateVisible(false);
  emits("done");
};
// 更改弹框状态
const updateVisible = (value) => {
  emits("update:visible", value);
};

//#- 保存
const loading = ref(false);

const save = () => {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      loading.value = true;
      if (isUpdate.value) {
        kfApi.edit(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
        });
      } else {
        kfApi.add(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          console.log("add:response", response);
        });
      }
      // setTimeout(() => {
       
      // }, 1000);

      loading.value = false;
      closeDialog();
    }
  });
};

</script>
